package com.jsz.peini.server.thread;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.jsz.peini.bean.comm.ImageFilterResult;
import com.jsz.peini.bean.report.ReportInfo;
import com.jsz.peini.bean.user.UserInfo;
import com.jsz.peini.common.util.ImageFilterUtil;
import com.jsz.peini.common.util.SpringContextUtil;
import com.jsz.peini.common.util.UploadQN;
import com.jsz.peini.mapper.comm.ImageFilterResultMapper;
import com.jsz.peini.mapper.report.ReportInfoMapper;
import com.jsz.peini.mapper.square.SquareImageMapper;
import com.jsz.peini.mapper.user.UserImageMapper;
import com.jsz.peini.mapper.user.UserInfoMapper;
import com.jsz.peini.redis.base.RedisConstants;
import com.jsz.peini.redis.base.RedisUtil;
import com.jsz.peini.redis.base.SerializeUtil;
import com.jsz.peini.vo.ImgVo;


public class ImgCheckThread extends Thread{
	
	Logger logger = Logger.getLogger(ImgCheckThread.class);
	
	public void init(){
		this.start();
	}
	
	@Override
	public void run() {
		super.run();
		
		ImageFilterResultMapper imageFilterResultMapper = (ImageFilterResultMapper)SpringContextUtil.getBean(ImageFilterResultMapper.class);
		SquareImageMapper squareImageMapper = (SquareImageMapper)SpringContextUtil.getBean(SquareImageMapper.class);
		UserImageMapper userImageMapper = (UserImageMapper)SpringContextUtil.getBean(UserImageMapper.class);
		ReportInfoMapper reportInfoMapper = (ReportInfoMapper)SpringContextUtil.getBean(ReportInfoMapper.class);
		UserInfoMapper userInfoMapper = (UserInfoMapper)SpringContextUtil.getBean(UserInfoMapper.class);
		
		while(true)
		{
			try {
				List<byte[]> list = RedisUtil.brpop(RedisConstants.IMG_CHECK.getBytes());
				if(list.size() > 1){
					byte [] img = list.get(1);
					ImgVo imgVo = (ImgVo)SerializeUtil.unserialize(img);
					Integer result = ImageFilterUtil.checkImg(imgVo);
					if(result != null){
						if(result == 1){
							ImageFilterResult image = new ImageFilterResult();
							image.setImgurl(imgVo.getImgUrl());
							switch (imgVo.getType()) {
							case 1:
								image.setUrltype("squareImage");
								break;
							case 2:
								image.setUrltype("imageHead");
								break;
							case 3:
								image.setUrltype("reportImage");
								break;
							case 4:
								image.setUrltype("userImage");
								break;
							case 5:
								image.setUrltype("spaceBgImg");
								break;
							default:
								break;
							}
							
							imageFilterResultMapper.insert(image);
						}else if(result == 2){
							UploadQN upload = new UploadQN();
							upload.delete(imgVo.getImgUrl());
							switch (imgVo.getType()) {
							case 1:
								squareImageMapper.deleteByPrimaryKey(Integer.parseInt(imgVo.getParamId()));
								break;
							case 2:
								UserInfo userInfo = userInfoMapper.selectByPrimaryKey(imgVo.getParamId());
								userInfo.setImageHead("");
								userInfoMapper.updateByPrimaryKeySelective(userInfo);
								break;
							case 3:
								ReportInfo info = reportInfoMapper.selectByPrimaryKey(Integer.parseInt(imgVo.getParamId()));
								if(StringUtils.isNotBlank(info.getReportImgs())){
									String imgSrc = info.getReportImgs();
									if(imgSrc.contains(imgVo.getImgUrl())){
										imgSrc = imgSrc.replace(imgVo.getImgUrl(), "");
									}
									info.setReportImgs(imgSrc);
									reportInfoMapper.updateByPrimaryKeySelective(info);
								}
								break;
							case 4:
								userImageMapper.deleteByPrimaryKey(Integer.parseInt(imgVo.getParamId()));
								break;
							case 5:
								userInfo = userInfoMapper.selectByPrimaryKey(imgVo.getParamId());
								userInfo.setSpaceBgImg("");
								userInfoMapper.updateByPrimaryKeySelective(userInfo);
								break;
							default:
								break;
							}
						}
					}
				}
				
				Thread.sleep(1);
			} catch (Exception e1) {
				e1.printStackTrace();
				logger.info("fail", e1);
			}
		}
		
	}
	
}